package org.vt.ece4564.pcw.textinclass;

import org.vt.ece4564.pcw.textinclass.R;

import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.app.Activity;

public class RegisterUserActivity extends Activity implements OnClickListener{
	
	private EditText usernameEditText_;
	private EditText nameEditText_;
	private EditText passwordEditText_;
	private EditText passwordReenterEditText_;
	private Button registerButton_;
	private ProgressBar progressBar_;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register_user);
        
        usernameEditText_ = (EditText) findViewById(R.id.registerUsernameEditText);
        nameEditText_ = (EditText) findViewById(R.id.registerNameEditText);
        passwordEditText_ = (EditText) findViewById(R.id.registerPasswordEditText);
        passwordReenterEditText_ = (EditText) findViewById(R.id.registerPasswordReenterEditText);
        registerButton_ = (Button) findViewById(R.id.registerButton);
        progressBar_ = (ProgressBar) findViewById(R.id.registerUserProgressBar);
        
        registerButton_.setOnClickListener(this);
        
        progressBar_.setEnabled(false);
        progressBar_.setVisibility(View.INVISIBLE);
        
    }

	public void onClick(View v) {
		
		String username = usernameEditText_.getText().toString();
		String name = nameEditText_.getText().toString();
		String password = passwordEditText_.getText().toString();
		String passwordReenter = passwordReenterEditText_.getText().toString();
		
		if (!password.equals(passwordReenter)) {
			Toast toast = Toast.makeText(getApplicationContext(), 
					password + " does not match " + passwordReenter, Toast.LENGTH_SHORT);
			
			toast.show();
			return;
		}
		
		registerButton_.setEnabled(false);
		progressBar_.setVisibility(View.VISIBLE);
		
		RegisterRequest registerRequest = new RegisterRequest(username, name, password,
				passwordReenter, this);
		
		registerRequest.execute();
		
	}
	
	public void resetScreen() {
		usernameEditText_.setText("");
		nameEditText_.setText("");
		passwordEditText_.setText("");
		passwordReenterEditText_.setText("");
		progressBar_.setVisibility(View.VISIBLE);
		registerButton_.setEnabled(true);
	}
    
}
